package com.shop.shoppingmall.service.impl;

import com.shop.shoppingmall.dao.ShProductCategoryDao;
import com.shop.shoppingmall.entity.ShProductCategory;
import com.shop.shoppingmall.service.ShProductCategoryService;
import com.shop.shoppingmall.util.UUIDGenerator;
import com.shop.shoppingmall.util.api.vo.Result;
import com.shop.shoppingmall.util.oConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品分类表(ShProductCategory)表服务实现类
 *
 * @author makejava
 * @since 2020-09-07 15:49:59
 */
@Service("shProductCategoryService")
@Transactional
public class ShProductCategoryServiceImpl implements ShProductCategoryService {
    @Resource
    private ShProductCategoryDao shProductCategoryDao;

    /**
     * 通过ID查询单条数据
     *
     * @param categoryId 主键
     * @return 实例对象
     */
    @Override
    public ShProductCategory queryById(String categoryId) {
        return this.shProductCategoryDao.queryById(categoryId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ShProductCategory> queryAllByLimit(int offset, int limit) {
        return this.shProductCategoryDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param shProductCategory 实例对象
     * @return 实例对象
     */
    @Override
    public Result insert(ShProductCategory shProductCategory) {
        String salt = oConvertUtils.randomGen(6);
        shProductCategory.setCategoryCode(salt);
        if(shProductCategory.getCategoryId().equals("0")){
            shProductCategory.setParentId(shProductCategory.getCategoryId());
            shProductCategory.setCategoryId(UUIDGenerator.generate());
            shProductCategory.setCategoryLevel("1");
            shProductCategory.setCategoryStatus("1");
            shProductCategoryDao.insertSelective(shProductCategory);
            return  Result.ok("添加成功");
        }else{
            ShProductCategory shProductCategory1 = shProductCategoryDao.queryById(shProductCategory.getCategoryId());
            if(shProductCategory1 != null){
                Integer level = Integer.valueOf(shProductCategory1.getCategoryLevel())+1;
                shProductCategory.setCategoryLevel(level.toString());
                shProductCategory.setParentId(shProductCategory.getCategoryId());
                shProductCategory.setCategoryId(UUIDGenerator.generate());
                shProductCategory.setCategoryStatus("1");
                shProductCategoryDao.insertSelective(shProductCategory);
                return  Result.ok("添加成功");
            }else{
                return Result.error("未查到该分类上级");
            }

        }


    }

    /**
     * 修改数据
     *
     * @param shProductCategory 实例对象
     * @return 实例对象
     */
    @Override
    public ShProductCategory update(ShProductCategory shProductCategory) {
        this.shProductCategoryDao.update(shProductCategory);
        return this.queryById(shProductCategory.getCategoryId());
    }

    /**
     * 通过主键删除数据
     *
     * @param categoryId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String categoryId) {
        return this.shProductCategoryDao.deleteById(categoryId) > 0;
    }



    @Override
    public Result listAll() {
        List<ShProductCategory> s = shProductCategoryDao.list();
        List<ShProductCategory> shProductCategories =  getPowers(s);
        return  Result.ok(shProductCategories);
    }


    /**
     * 在list中寻找指定parentid的数据 并返回
     * @param list 全部权限
     * @param parentId 父级id
     * @return 所属父级的全部权限
     */
    private List<ShProductCategory> searchListByParentId(List<ShProductCategory> list,String parentId){
        List<ShProductCategory> temp = new ArrayList<>();
        for(ShProductCategory p : list){
            if(p.getParentId().equals(parentId)){
                temp.add(p);
            }
        }
        return temp;
    }

    private List<ShProductCategory> getPowers(List<ShProductCategory> p) {
        List<ShProductCategory> menu = searchListByParentId(p,"0");
        for(ShProductCategory pp : menu){
            pp.setChildren(searchListByParentId(p,pp.getCategoryId()));
            for(ShProductCategory ppp : pp.getChildren()){
                ppp.setChildren(searchListByParentId(p,ppp.getCategoryId()));
            }
        }
        return menu;
    }

    @Override
    public Result queryByParentId() {
        List<ShProductCategory> categoryList = shProductCategoryDao.queryByParentId();
        return  Result.ok(categoryList);
    }

    @Override
    public Result selectParentId(String parentId) {
        List<ShProductCategory> categoryList = shProductCategoryDao.selectParentId(parentId);
        return  Result.ok(categoryList);
    }

    @Override
    public Result deleteParentId(String categoryId) {
        ShProductCategory shProductCategory = shProductCategoryDao.queryById(categoryId);
        if(shProductCategory != null || shProductCategory.getCategoryLevel().equals("1")){
            if(shProductCategoryDao.selectParentId(shProductCategory.getCategoryId()).size()>0){
                shProductCategoryDao.deleteById(shProductCategory.getCategoryId());
                shProductCategoryDao.deleteSecond(shProductCategory.getCategoryId());
                return  Result.ok("已删除一级分类及下级分类");
            }else{
                shProductCategoryDao.deleteById(shProductCategory.getCategoryId());
                return  Result.ok("已删除一级分类");
            }

        }else if(shProductCategory != null || shProductCategory.getCategoryLevel().equals("2")){
            if(shProductCategoryDao.selectParentId(shProductCategory.getCategoryId()).size()>0){
                shProductCategoryDao.deleteById(shProductCategory.getCategoryId());
                shProductCategoryDao.deleteThird(shProductCategory.getCategoryId());
                return  Result.ok("已删除二级分类及下级分类");
            }else{
                shProductCategoryDao.deleteById(shProductCategory.getCategoryId());
                return  Result.ok("已删除二级分类");
            }

        }else{
            shProductCategoryDao.deleteById(shProductCategory.getCategoryId());
            return  Result.ok("已删除三级分类");
        }
    }
}